﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReceiptTracker
{
    public class PieChart
    {
        #region ChartItemClass
        public class ChartItem
        {
            public double Value { get; set; }
            public string Label { get; set; }

            public ChartItem(int value, string label)
            {
                Value = Convert.ToDouble(value);
                Label = label;
            }
            public ChartItem(float value, string label)
            {
                Value = Convert.ToDouble(value);
                Label = label;
            }
            public ChartItem(double value, string label)
            {
                Value = value;
                Label = label;
            }
        } 
        #endregion

        private List<ChartItem> items = new List<ChartItem>();
        private Graphics graphics;
        private Font font = new Font(FontFamily.GenericSansSerif, 16, FontStyle.Bold);
        private Brush brushFont = new SolidBrush(Color.Black);
        public string Header { get; set; }
        public string ValueUnit { get; set; }
        private Color[] colors = { Color.Red, Color.Blue, Color.Yellow, Color.Green, Color.Magenta, Color.Brown, Color.Turquoise, Color.Orange, Color.Lime };

        public PieChart(Graphics graphics, string header = "", string valueunit = "")
        {
            this.graphics = graphics;
            Header = header;
            ValueUnit = valueunit;
        }

        public PieChart(Graphics graphics, List<ChartItem> items, string header = "", string valueunit = "")
        {
            this.graphics = graphics;
            Header = header;
            ValueUnit = valueunit;
            this.items = items;
        }

        public void AddItem(ChartItem item)
        {
            items.Add(item);
        }
        public void AddItem(double value, string label)
        {
            items.Add(new ChartItem(value, label));
        }

        public void ClearItems()
        {
            items.Clear();
        }

        public void DrawChart(int x, int y, int width, int height)
        {            
            double total = 0;
            foreach (ChartItem item in items)
            {
                total += item.Value;
            }

            double deg = 0;
            double degSum = 0;
            int i = 0;
            Rectangle rect = new Rectangle(x, y + 40, width, height);            
            graphics.DrawString(Header, font, brushFont, x, y);

            foreach (ChartItem item in items)
            {
                deg = (item.Value / total) * 360;
                Brush brush;
                try
                {
                    brush = new SolidBrush(colors[i]);
                }
                catch
                {
                    brush = new SolidBrush(colors[i - colors.Length + 1]);
                }
                graphics.FillPie(brush, rect, Convert.ToSingle(degSum), Convert.ToSingle(deg));
                graphics.FillRectangle(brush, x + width + 20, y + 20 * i + 5, 15, 15);
                graphics.DrawString(item.Label + ": " + item.Value + ValueUnit, font, brushFont, x + width + 35, y + 20 * i);
                degSum += deg;        
                i++;
            }
        }
    }
}
